//
//  KARepayDetailBankCell.swift
//  kano
//
//  Created by Frank on 2024/9/19.
//

import UIKit

class KARepayDetailBankCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.contentView.backgroundColor = .white
    }
    
    lazy var var_bgView: UIView = {
        let var_bgView = UIView();
        var_bgView.backgroundColor = .colorf2f2f5
        return var_bgView
    }()
    lazy var var_titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(13), weight: .bold)
        label.textColor = .colorAEACB3
        label.text = "1st phase"
        return label
    }()
    lazy var var_contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(13), weight: .bold)
        label.textColor = .color323135
        label.text = "STERLING BANK"
        return label
    }()
    lazy var var_copyBtn: UIButton = {
        let button = UIButton()
        button.setTitle(" Copy", for: .normal)
        button.setTitleColor(.themeColor, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(10))
        button.setImage(UIImage(named: "common_copy"), for: .normal)
        button.addTarget(self, action: #selector(copyAccount), for: .touchUpInside)
        return button
    }()
 
    @objc func copyAccount() {
        UIPasteboard.general.string = var_contentLabel.text
        UIViewController.ka_current().view.makeToast("copy success")
    }
    
    static func ka_cell(for tableView: UITableView) -> KARepayDetailBankCell {
        let var_identifityId = NSStringFromClass(self)
        var var_cell = tableView.dequeueReusableCell(withIdentifier: var_identifityId)
        if var_cell == nil {
            var_cell = KARepayDetailBankCell.init(style: .default, reuseIdentifier: var_identifityId)
        }
        return var_cell as! KARepayDetailBankCell
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var cellModel: KARowInfo?
    
    var showSubSender: Bool {
        get {
            false
        }
        set {
            if newValue {
                var_copyBtn.isHidden = false
                var_contentLabel.snp.remakeConstraints { make in
                    make.right.equalTo(var_copyBtn.snp.left).offset(kscale(-8));
                    make.centerY.equalTo(var_bgView);
                }
            } else {
                var_copyBtn.isHidden = true
                var_contentLabel.snp.remakeConstraints { make in
                    make.right.equalTo(var_bgView).offset(kscale(-10));
                    make.centerY.equalTo(var_bgView);
                }
            }
        }
    }
}

extension KARepayDetailBankCell: UITextFieldDelegate {
    func ka_setUI() {
        backgroundColor = .white
        contentView.addSubview(var_bgView)
        var_bgView.addSubview(var_titleLabel)
        var_bgView.addSubview(var_contentLabel)
        var_bgView.addSubview(var_copyBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var_bgView.snp.makeConstraints { make in
            make.top.bottom.equalTo(contentView)
            make.left.equalTo(contentView).offset(kscale(16));
            make.right.equalTo(contentView).offset(kscale(-16));
        }
        var_titleLabel.snp.makeConstraints { make in
            make.left.equalTo(var_bgView).offset(kscale(10));
            make.centerY.equalTo(var_bgView);
        }
        var_copyBtn.snp.makeConstraints { make in
            make.right.equalTo(var_bgView).offset(kscale(-10));
            make.size.equalTo(CGSizeMake(kscale(54), kscale(18)))
            make.centerY.equalTo(var_bgView);
        }
        var_contentLabel.snp.makeConstraints { make in
            make.centerY.equalTo(var_bgView);
            make.right.equalTo(var_copyBtn.snp.left).offset(kscale(-8));
        }
        
        var_copyBtn.layer.borderWidth = 0.5
        var_copyBtn.layer.borderColor = UIColor.themeColor.cgColor
        var_copyBtn.layer.cornerRadius = kscale(9)
        var_copyBtn.layer.masksToBounds = true
        
        guard let model = cellModel else { return }
        if (model.isFirst && model.isLast) {
            var_bgView.ka_addCorner(withRadius: kscale(10))
        } else {
            if (model.isFirst) {
                var_bgView.ka_addTopCorner(withRadius: kscale(10))
            }
            if (model.isLast) {
                var_bgView.ka_addBottomCorner(withRadius: kscale(10))
            }
        }
    }
}

